package 力扣日常刷题.木23一月.第03天303;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 帅小伙
 * @date 2022/3/3
 * @description
 */
public class Demo27全排列 {

    List<List<Integer>> ans = new ArrayList<>();
    boolean[] isVisited;
    public List<List<Integer>> permute(int[] nums) {
        isVisited = new boolean[nums.length];
        dfs(nums,new ArrayList<>());
        return ans;
    }

    public void dfs(int[] nums,ArrayList<Integer> path){
        if(path.size() == nums.length) ans.add(new ArrayList<>(path));

        for (int i = 0; i < nums.length; i++) {
            if(!isVisited[i]){
                path.add(nums[i]);
                isVisited[i] = true;
                dfs(nums,path);
                path.remove(path.size() - 1);
                isVisited[i] = false;
            }
        }

    }
}
